//
//  NSObject+SLSafe.m
//  SLKit
//
//  Created by S_LINE on 2020/9/29.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "NSObject+SLSafe.h"

#import "SLSafe.h"

@implementation NSObject (SLSafe)

#pragma mark - NSString安全方法
- (NSString *)trimForString:(NSString *)trimString
{
    if (![SLSafe isEffectiveString:self])
    {
        return @"";
    }
    if (![SLSafe isEffectiveString:trimString])
    {
        return (NSString *)self;
    }
    NSString *string = (NSString *)self;
    string = [string stringByReplacingOccurrencesOfString:trimString withString:@""];
    return string;
}

- (BOOL)sl_isEqualToString:(NSString *)string
{
    if (![SLSafe isEffectiveString:string] || ![SLSafe isEffectiveString:self])
    {
        return NO;
    }
    BOOL result = [string isEqualToString:(NSString *)self];
    return result;
}

- (BOOL)sl_containsString:(NSString *)string
{
    if (![SLSafe isEffectiveString:string] || ![SLSafe isEffectiveString:self])
    {
        return NO;
    }
    BOOL result = [(NSString *)self containsString:string];
    return result;
}

- (BOOL)sl_hasPrefix:(NSString *)string
{
    if (![SLSafe isEffectiveString:string] || ![SLSafe isEffectiveString:self])
    {
        return NO;
    }
    BOOL result = [(NSString *)self hasPrefix:string];
    return result;
}

- (BOOL)sl_hasSuffix:(NSString *)string
{
    if (![SLSafe isEffectiveString:string] || ![SLSafe isEffectiveString:self])
    {
        return NO;
    }
    BOOL result = [(NSString *)self hasSuffix:string];
    return result;
}

#pragma mark - NSArray安全方法
- (nullable id)sl_objectAtIndex:(NSUInteger)index
{
    if (![self isKindOfClass:[NSArray class]])
    {
        return nil;
    }
    NSArray *array = (NSArray *)self;
    if (index >= array.count) {
        return nil;
    }
    id obj = [array objectAtIndex:index];
    return obj;
}

- (BOOL)sl_containsObject:(id _Nullable)obj
{
    if (![self isKindOfClass:[NSArray class]])
    {
        return NO;
    }
    if (!obj)
    {
        return NO;
    }
    NSArray *array = (NSArray *)self;
    return [array containsObject:obj];
}

#pragma mark - NSMutableArray安全方法
- (BOOL)isMutableArray
{
    if ([self isKindOfClass:[NSMutableArray class]] || [self isKindOfClass:NSClassFromString(@"__NSArrayM")]) {
        return YES;
    }
    return NO;
}

- (void)sl_addObject:(id)object
{
    if (!object)
    {
        return;
    }
    if (![self isMutableArray])
    {
        return;
    }
    NSMutableArray *mutableArray = (NSMutableArray *)self;
    [mutableArray addObject:object];
}

- (void)sl_insertObject:(id)object atIndex:(NSUInteger)index
{
    if (!object)
    {
        return;
    }
    if (![self isMutableArray])
    {
        return;
    }
    NSMutableArray *mutableArray = (NSMutableArray *)self;
    if (index > mutableArray.count)
    {
        return;
    }
    [mutableArray insertObject:object atIndex:index];
}

- (void)sl_removeLastObject
{
    if (![self isMutableArray])
    {
        return;
    }
    NSMutableArray *mutableArray = (NSMutableArray *)self;
    if (mutableArray.count == 0)
    {
        return;
    }
    [mutableArray removeLastObject];
}

- (void)sl_removeObjectAtIndex:(NSUInteger)index
{
    if (![self isMutableArray])
    {
        return;
    }
    NSMutableArray *mutableArray = (NSMutableArray *)self;
    if (index >= mutableArray.count)
    {
        return;
    }
    [mutableArray removeObjectAtIndex:index];
}

- (void)sl_removeObject:(id)obj
{
    if (![self isMutableArray])
    {
        return;
    }
    NSMutableArray *mutableArray = (NSMutableArray *)self;
    if (![mutableArray containsObject:obj])
    {
        return;
    }
    [mutableArray removeObject:obj];
}

- (void)sl_replaceObjectAtIndex:(NSUInteger)index withObject:(id)object
{
    if (!object)
    {
        return;
    }
    if (![self isMutableArray])
    {
        return;
    }
    NSMutableArray *mutableArray = (NSMutableArray *)self;
    if (index >= mutableArray.count)
    {
        return;
    }
    [mutableArray replaceObjectAtIndex:index withObject:object];
}

#pragma mark - NSDictionary安全方法
- (nullable id)sl_objectForKey:(id<NSCopying>)key
{
    if (!key)
    {
        return nil;
    }
    if (![self isKindOfClass:[NSDictionary class]])
    {
        return nil;
    }
    NSDictionary *dict = (NSDictionary *)self;
    if (![dict.allKeys containsObject:key])
    {
        return nil;
    }
    id obj = [dict objectForKey:key];
    return obj;
}

- (nullable NSString *)sl_stringForKey:(id<NSCopying>)key
{
    id obj = [self sl_objectForKey:key];
    if ([obj isKindOfClass:[NSString class]])
    {
        return obj;
    }
    else if ([obj isKindOfClass:[NSNumber class]])
    {
        return [NSString stringWithFormat:@"%@", obj];
    }
    else
    {
        return nil;
    }
}

- (nullable NSDecimalNumber *)sl_decimalNumberForKey:(id<NSCopying>)key
{
    NSString *string = [self sl_stringForKey:key];
    if ([SLSafe isEffectiveString:string])
    {
        NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:string];
        if ([decimalNumber isKindOfClass:[NSDecimalNumber class]])
        {
            return decimalNumber;
        }
    }
    return nil;
}

- (BOOL)sl_containsKey:(id<NSCopying>)key
{
    if (![SLSafe isEffectiveDictionary:self])
    {
        return NO;
    }
    return [((NSDictionary *)self).allKeys sl_containsObject:key];
}

#pragma mark - NSMutableDictionary安全方法
- (BOOL)isMutableDictionary
{
    if ([self isKindOfClass:[NSMutableDictionary class]] || [self isKindOfClass:NSClassFromString(@"__NSDictionaryM")]) {
        return YES;
    }
    return NO;
}

- (void)sl_removeObjectForKey:(id<NSCopying>)key
{
    if (!key)
    {
        return;
    }
    if (![self isMutableDictionary])
    {
        return;
    }
    NSMutableDictionary *mutableDict = (NSMutableDictionary *)self;
    if (![mutableDict.allKeys containsObject:key])
    {
        return;
    }
    [mutableDict removeObjectForKey:key];
}

- (void)sl_setObject:(id)object forKey:(id<NSCopying>)key
{
    if (!key)
    {
        return;
    }
    if (!object) {
        return;
    }
    if (![self isMutableDictionary])
    {
        return;
    }
    NSMutableDictionary *mutableDict = (NSMutableDictionary *)self;
    [mutableDict setObject:object forKey:key];
}

@end
